import json

from django.http import JsonResponse
import os
from .models import MetroRoute, MetroStop


# Create your views here.
def receive_metro(request):
	try:
		file = request.FILES.get('file')
		from django.core.files.storage import FileSystemStorage
		print(file.name)
		fs = FileSystemStorage()
		fs.save(file.name, file)
		return JsonResponse({'code:': 1, 'success': True, 'msg': '上传成功'})
	except:
		return JsonResponse({'code:': 0, 'success': False, 'msg': '上传失败'})


def receive_stop(request):
	try:
		file = request.FILES.get('file')
		from django.core.files.storage import FileSystemStorage
		print(file.name)
		fs = FileSystemStorage()
		fs.save(file.name, file)
		return JsonResponse({'code': 1, 'success': True, 'msg': '上传成功'})
	except:
		return JsonResponse({'code': 0, 'success': False, 'msg': '上传失败'})


def generate_metro_table(request):
	if not os.path.exists('./MetroRoute.txt'):
		return JsonResponse({'code:': 0, 'success': False, 'msg': '请上传地铁路线文件'})
	else:
		with open('./MetroRoute.txt', 'r+', encoding='utf-8-sig') as f:
			# try:
			for line in f.readlines():
				line = line.strip()
				route, _, station = line.split('|')
				station_each = station.split(',')
				start_end = station_each[0] + '->' + station_each[-1]
				print(start_end)
				MetroRoute(route=route, start_end=start_end, station=station).save()
			f.close()
			data = MetroRoute.objects.all().values()
			table_data = list(data)
			return JsonResponse({'code': 1, 'success': True, 'msg': '生成完毕', 'tableData': table_data})
			# except:
			f.close()
			return JsonResponse({'code': -1, 'success': False, 'msg': '数据入库错误'})


def generate_stop_table(request):
	if not os.path.exists('./MetroStop.txt'):
		return JsonResponse({'code:': 0, 'success': False, 'msg': '请上传地铁站点文件'})
	else:
		with open('./MetroStop.txt', 'r+', encoding='utf-8-sig') as f:
			try:
				for line in f.readlines():
					line = line.strip()
					number, station_name, location, route, route_common = line.split("|")
					MetroStop(number=number, station_name=station_name, location=location, route=route,
							  route_common=route_common).save()
				f.close()
				data = MetroStop.objects.all().values()
				table_data = list(data)
				return JsonResponse({'code': 1, 'success': True, 'msg': '生成完毕', 'tableData': table_data})
			except:
				f.close()
				return JsonResponse({'code': -1, 'success': False, 'msg': '数据入库错误'})


def get_metro_table(request):
	json_param = json.loads(request.body.decode('utf-8'))
	pagesize = int(json_param.get('pagesize'))
	current_page = int(json_param.get('currentPage'))
	total = MetroRoute.objects.all().count()
	data = MetroRoute.objects.all().values()
	table_data = list(data)[(current_page - 1) * pagesize:current_page * pagesize]
	return JsonResponse({'code': 1, 'success': True, 'msg': '生成完毕', 'tableData': table_data, 'total': total})


def get_stop_table(request):
	json_param = json.loads(request.body.decode('utf-8'))
	pagesize = int(json_param.get('pagesize'))
	current_page = int(json_param.get('currentPage'))
	print(pagesize, current_page)
	total = MetroStop.objects.all().count()
	data = MetroStop.objects.all().values()
	table_data = list(data)[(current_page - 1) * pagesize:current_page * pagesize]
	return JsonResponse({'code': 1, 'success': True, 'msg': '生成完毕', 'tableData': table_data, 'total': total})
